﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NodeEditer;
using UnityEngine;
using UnityEditor;

namespace BehaviorSystem
{
    public class BehaviourEditorNodeDrawer : EditorNodeDrawerBase
    {
        Vector2 offset = new Vector2(20, 20);

        public void InitLayout()
        {
            windowRect.x = NodeEditorWindow.RightRect.width / 2;
            windowRect.y = NodeEditorWindow.RightRect.height / 20;
        }

        public override void DrawNode(object data,Rect rect)
        {

            NodeData node = (NodeData)data;
            rect =GUILayout.Window(node.Id, rect, DrawNodeWindow, "My Window");


            if (NodeEditorWindow.NodeId2NodeDataRects.ContainsKey(node.Id))
            {
                NodeEditorWindow.NodeId2NodeDataRects[node.Id].WindowRect = rect;
            }
            else
            {
                Debug.LogError("NodeId2NodeDataRects Not Contains  node,nodeId is " + node.Id);
            }

            if (node.ChildNodes.Count > 0)
            {
                DrawLineNode2ChildNode(node);

                int count = node.ChildNodes.Count;
                float initOffset=0;
                float curLayerNeedWidth = 0;

                for (int i = 0; i < count; i++)
                {
                    curLayerNeedWidth+= NodeEditorWindow.NodeId2NodeDataRects[node.ChildNodes[i].Id].NeedWidth;
                }

                for (int i = 0; i < count; i++)
                {
                    Rect childRect;
                    float curNodeNeedWidth = NodeEditorWindow.NodeId2NodeDataRects[node.ChildNodes[i].Id].NeedWidth;
                    if (i == 0)
                    {
                        initOffset -= offset.x/2;
                    }
                    else 
                    {
                        initOffset += offset.x;
                    }

                    Debug.Log("node.Id " + node.ChildNodes[i].Id + "   initOffset  " + initOffset);
                    childRect = new Rect(rect.x - (curLayerNeedWidth / 2) + curNodeNeedWidth / 2 + initOffset , rect.y + rect.height*2 + offset.y, rect.width, rect.height);
                    initOffset += curNodeNeedWidth;
                    DrawNode(node.ChildNodes[i], childRect);
                }
            }
        }

        public void DrawLineNode2ChildNode(NodeData node)
        {
            Handles.color = Color.black;
            Rect startRect;
            Rect endRect;
            if (node.ChildNodes.Count == 1) 
            {
                startRect = NodeEditorWindow.NodeId2NodeDataRects[node.Id].WindowRect;
                endRect= NodeEditorWindow.NodeId2NodeDataRects[node.ChildNodes[0].Id].WindowRect;
                Vector3 verticalStartPoint = new Vector3(startRect.x + startRect.width / 2, startRect.y + startRect.height);
                Vector3 verticalEndPoint = new Vector3(endRect.x + endRect.width / 2, endRect.y );

                Handles.DrawLine(verticalStartPoint, verticalEndPoint);
                return;
            }

            //多个子节点时，绘制横线
            startRect = NodeEditorWindow.NodeId2NodeDataRects[node.ChildNodes[0].Id].WindowRect;
            endRect = NodeEditorWindow.NodeId2NodeDataRects[node.ChildNodes.Last().Id].WindowRect;
            Vector3 horizontalStartPoint = new Vector3(startRect.x+startRect.width/2, startRect.y - (startRect.height + offset.y) / 2, 0);
            Vector3 horizontalEndPoint = new Vector3(endRect.x + endRect.width / 2, endRect.y - (endRect.height + offset.y) / 2, 0);
            Handles.DrawLine(horizontalStartPoint, horizontalEndPoint);

            //父节点引出 竖线
            startRect = NodeEditorWindow.NodeId2NodeDataRects[node.Id].WindowRect;
            endRect = NodeEditorWindow.NodeId2NodeDataRects[node.Id].WindowRect;
            Vector3 parentNodeverticalStartPoint = new Vector3(startRect.x + startRect.width / 2, startRect.y + startRect.height);
            Vector3 parentNodeverticalEndPoint = new Vector3(endRect.x + endRect.width / 2, endRect.y+endRect.height +(endRect.height+ offset.y)/2);
            

            Handles.DrawLine(parentNodeverticalStartPoint, parentNodeverticalEndPoint);

            for (int i = 0; i < node.ChildNodes.Count; i++)
            {
                startRect = NodeEditorWindow.NodeId2NodeDataRects[node.ChildNodes[i].Id].WindowRect;
                endRect = NodeEditorWindow.NodeId2NodeDataRects[node.ChildNodes[i].Id].WindowRect;
                Vector3 childNodeverticalStartPoint = new Vector3(startRect.x + startRect.width / 2, startRect.y);
                Vector3 childNodeverticalEndPoint = new Vector3(endRect.x + endRect.width / 2, endRect.y - (endRect.height + offset.y) / 2);
                Handles.DrawLine(childNodeverticalStartPoint, childNodeverticalEndPoint);

            }
        }

        public void ComputeNodeNeedWidth(NodeData node,float width)
        {
            
            if (!NodeEditorWindow.NodeId2NodeDataRects.ContainsKey(node.Id))
            {
                EditorNodeData nodeData = new EditorNodeData();
                nodeData.CurNodeData = node;
                NodeEditorWindow.NodeId2NodeDataRects.Add(node.Id, nodeData);
            }

            int count = node.ChildNodes.Count;
            for (int i = 0; i < count; i++)
            {
                ComputeNodeNeedWidth(node.ChildNodes[i], width);
            }
            if (count==0)
            {
                NodeEditorWindow.NodeId2NodeDataRects[node.Id].NeedWidth = width;
            }
            else
            {
                float needWith = 0;
                for (int i = 0; i < count; i++)
                {
                    needWith += NodeEditorWindow.NodeId2NodeDataRects[node.ChildNodes[i].Id].NeedWidth;
                }
                NodeEditorWindow.NodeId2NodeDataRects[node.Id].NeedWidth = needWith;
            }

        }
        
        protected override void DrawNodeWindow(int id)
        {
            GUILayout.Label("Dialogue Node - ID: "+id);
            if (GUILayout.Button("xxx"))
            {

            }
            Event e = Event.current;
            Rect windowRect = NodeEditorWindow.NodeId2NodeDataRects[id].WindowRect;
            //Debug.Log(GUIUtility.hotControl);


            if (e.button == 1 && e.type == EventType.MouseDown)
            {
                NodeEditorWindow.CurSelectedNodeId = id;
                if (windowRect.Contains(e.mousePosition+ windowRect.position))
                {
                    NodeEditorWindow.ContextClickPos = e.mousePosition + windowRect.position;

                }
            }

            if (e.type == EventType.MouseDown)
            {
                NodeEditorWindow.selectedNode.Add(id);
            }

            if (e.type == EventType.MouseUp)
            {
                if (NodeEditorWindow.NodeId2NodeDataRects[id].WindowRect.Contains(Event.current.mousePosition))
                {
                    NodeEditorWindow.selectedNode.Remove(id);
                }

            }

            if (e.type == EventType.MouseDrag)
            {
                Debug.Log("id  " + id);

                if (NodeEditorWindow.selectedNode.Contains(id))
                {
                    Rect temp = NodeEditorWindow.NodeId2NodeDataRects[id].WindowRect;
                    temp.x += e.delta.x;
                    temp.y += e.delta.y;
                    NodeEditorWindow.NodeId2NodeDataRects[id].WindowRect = temp;
                    if (id==1)
                    {
                        Debug.Log(NodeEditorWindow.NodeId2NodeDataRects[id]);
                    }
                    NodeEditorWindow.Repaint();
                }
            }


            //Debug.LogError("DrawTest"+ id);
            //GUI.backgroundColor = Color.black;
            //GUI.contentColor = Color.red;
            //GUI.color = new Color(0, 0, 0, 0.2f);
            //GUILayout.BeginVertical(GUI.skin.box);

            //Color lastc = GUI.contentColor;



            //GUILayout.BeginHorizontal();
            //GUILayout.FlexibleSpace();
            //GUI.skin.label.fontStyle = FontStyle.Bold;
            //GUILayout.Label("Dialogue Node - ID: " );
            //GUI.skin.label.fontStyle = FontStyle.Normal;
            //GUILayout.FlexibleSpace();
            //GUILayout.EndHorizontal();

            //GUI.contentColor = lastc;

            //Event e = Event.current;
            //GUILayout.BeginHorizontal();

            //string delText = "Delete Node";
            //if (GUILayout.Button(delText, GUILayout.Width(80)))
            //{

            //}
            //if (e.type == EventType.MouseDown)
            //{
            //    //areYouSure = false;
            //    Repaint();
            //}

            ////GUI.color = defaultColor;
            ////if (GUILayout.Button("Add comment", GUILayout.Width(140)))
            ////{
            ////    areYouSure = false;
            ////    addComment(db.playerDiags[id]);
            ////    needSave = true;
            ////}

            //string isp = "Is Player";

            //GUILayout.EndHorizontal();

            //GUILayout.EndVertical();
        }
    }
}
